import reactRefresh from '@vitejs/plugin-react-refresh'
import { ConfigEnv, UserConfig } from 'vite'
import path, { join } from 'path'
import vitePluginImp from 'vite-plugin-imp'
import fs from 'fs'
//@ts-ignore
import lessToJS from 'less-vars-to-js'

const srcRoot = join(__dirname, 'src')
const assetsRoot = join(__dirname, 'assets')
const themeVariables = lessToJS(
    fs.readFileSync(path.resolve(__dirname, './config/variables.less'), 'utf8')
)

export default ({ command }: ConfigEnv): UserConfig => {
    console.log("### command -> ", command, __dirname);
    // DEV
    if (command === 'serve') {
        return {
            base: '/',
            mode: 'development',
            plugins: [
                reactRefresh(),
                // vitePluginImp({
                //   libList: [
                //     {
                //       libName: "antd",
                //       style: (name: string) => `antd/lib/${name}/style/index.less`,
                //     },
                //   ],
                // })
            ],
            css: {
                preprocessorOptions: {
                    less: {
                        javascriptEnabled: true,
                        modifyVars: themeVariables
                    }
                }
            },
            alias: {
                '/@': srcRoot,
            },
            build: {
                outDir: join(srcRoot, '/out'),
                emptyOutDir: true,
                sourcemap: false,
                rollupOptions: {},
            },
            server: {
                port: 8094,
                proxy: {
                    '/api': {
                        target: 'http://127.0.0.1:8092/',
                        changeOrigin: true,
                        // rewrite: (path: string) => path.replace('^/api', ''),
                    },
                    '/user': {
                        target: 'http://127.0.0.1:8092/',
                        changeOrigin: true,
                        // rewrite: (path: string) => path.replace('^/jApi', ''),
                    }
                }
            },
            optimizeDeps: {
                auto: true,
                exclude: ['path'],
            },
        };
    }
    // PROD
    else {
        return {
            base: "./",
            plugins: [
                reactRefresh(),
                vitePluginImp({
                    libList: [
                        {
                            libName: "antd",
                            style: (name: string) => `antd/lib/${name}/style/index.less`,
                        },
                    ],
                })
            ],
            css: {
                preprocessorOptions: {
                    less: {
                        // 支持内联 JavaScript
                        javascriptEnabled: true,
                        // 重写 less 变量，定制样式
                        modifyVars: themeVariables
                    }
                }
            },
            alias: {
                '/@': srcRoot,
            },
            build: {
                outDir: join(srcRoot, '../out'),
                emptyOutDir: true,
                rollupOptions: {},
            },
            optimizeDeps: {
                auto: true,
                exclude: ['path'],
            },
        };
    }
};
